package com.example.uidemo_01.Test;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.uidemo_01.R;

public class StudyActivity extends AppCompatActivity {

    //声明控件
    private RadioGroup mRG1;
    private CheckBox mCB1;
    private CheckBox mCB2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_study);

        //找到控件
        mRG1 = findViewById(R.id.rg_1);
        mCB1 = findViewById(R.id.cb_1);
        mCB2 = findViewById(R.id.cb_2);

        //设置点击事件
        mRG1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //在radioButton里面找单选框
                RadioButton radioButton = group.findViewById(checkedId);
                Toast.makeText(StudyActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });

        mCB1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(StudyActivity.this, isChecked?"选中":"未选中", Toast.LENGTH_SHORT).show();
            }
        });
        mCB2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Toast.makeText(StudyActivity.this, isChecked?"选中":"未选中", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
